﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using Windows.UI;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Centny
{
    //public interface GACGroupable
    //{
    //    ICollection<GACItem> Items { get; set; }
    //}
    public class GACItem
    {
        public IList<Object> RItems { get; set; }
    }
    public delegate void PrepareAutoCanvas(AutoCanvas ac, GridViewExt gview, DependencyObject element, object item);
    public sealed partial class GroupAutoCanvas : UserControl
    {
        public DataTemplate GroupHeaderTemplate
        {
            get
            {
                return this._GroupGView.GroupHeaderTemplate;
            }
            set
            {
                this._GroupGView.GroupHeaderTemplate = value;
            }
        }
        public DataTemplateSelector GroupHeaderTemplateSelector
        {
            get
            {
                return this._GroupGView.GroupHeaderTemplateSelector;
            }
            set
            {
                this._GroupGView.GroupHeaderTemplateSelector = value;
            }
        }
        public Object GroupItems
        {
            get;
            set;
        }
        public PrepareAutoCanvas OnPrepareAutoCanvas { get; set; }
        public String ItemsPath
        {
            get
            {
                return this._GroupGView.ItemsPath;
            }
            set
            {
                this._GroupGView.ItemsPath = value;
            }
        }
        public GroupAutoCanvas()
        {
            this.InitializeComponent();
            this._GroupGView.OnPrepareItemContainer = OnPrepareItemContainer;
            this._GroupGView.HaveOverStyle = false;
            this.Loaded += OnLoaded;
        }
        public void OnLoaded(object sender, RoutedEventArgs e)
        {
            this._GroupGView.GroupItems = this.GroupItems;
        }
        private Dictionary<Object, AutoCanvas> Item2Canvas = new Dictionary<Object, AutoCanvas>();
        public void OnPrepareItemContainer(GridViewExt gview, DependencyObject element, object item)
        {
            AutoCanvas ac;
            GridViewItem gvi = element as GridViewItem;
            if (this.Item2Canvas.ContainsKey(item))
            {
                ac = this.Item2Canvas[item];
                gvi.Content = ac;
                return;
            }
            GACItem ritem = item as GACItem;
            ac = new AutoCanvas();
            if (this.OnPrepareAutoCanvas == null)
            {
                throw new InvalidDataException("the OnPrepareAutoCanvas handler not initial");
            }
            ac.Height = this._GroupGView.ActualHeight - 80;
            this.OnPrepareAutoCanvas(ac, gview, element, item);
            ac.ItemSources = ritem.RItems;
            ac.Scrollable = false;
            ac.LayoutElements();
            gvi.Width = ac.Width;
            gvi.Height = ac.Height;
            ac.Visibility = Visibility.Visible;
            gvi.Content = ac;
            this.Item2Canvas[item] = ac;
        }
    }
}
